/**
 * 文 件 名:  ModuleController.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.czd.support.model.Channel;
import com.czd.support.model.Manager;
import com.czd.support.model.Module;
import com.czd.support.model.TreeModel;
import com.czd.support.service.ChannelService;
import com.czd.support.service.ModuleService;
import com.czd.support.util.ResponseUtils;

/**
 * @Description 模块管理控制类
 * 
 * @Author Jun.Yang
 * @Date 2015年1月23日 上午10:29:54
 * @Version v1.0
 */
@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController {
	@Autowired
	private ModuleService moduleService;
	@Autowired
	private ChannelService channelService;
	
	@RequestMapping("/list")
	public String list() {
		return "/module/list";
	}

	/**
	 * 加载模块树
	 * 
	 * @param response
	 */
	@RequestMapping(value = "/treeAll", method = RequestMethod.POST)
	@ResponseBody
	public void treeAll(HttpServletResponse response) {
		List<TreeModel> channelTree = moduleService.createModuleTree();
		String jsonStr = JSON.toJSONString(channelTree);
		ResponseUtils.renderJson(response, jsonStr);

	}

	/**
	 * 获取节点下的所有子节点数据
	 * 
	 * @param level
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/modules/{level}", method = RequestMethod.GET)
	public String treeByParent(@PathVariable String level, Model model) {
		List<Module> moduleList = moduleService.listByParent(level);
		model.addAttribute("moduleList", moduleList);

		return "module/list_child";
	}


	/**
	 * 跳转到模块添加界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request, Model model) {
		String level = request.getParameter("level");
		if(StringUtils.isNotBlank(level)) {
			List<Channel> channelList = channelService.listByParent(level);
			model.addAttribute("channelList", channelList);
			
			return "module/add";
		}
		
		return error(model, "操作失败", "模块栏目信息失败");
	}

	/**
	 * 模块添加
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Module module, HttpServletRequest request, Model model) {
		Manager manager = this.getSesionUser(request);
		module.setCreateUser(manager.getUserCode());
		module.setUpdateUser(manager.getUserCode());
		if (moduleService.insert(module)) {
			return "redirect:modules/"+module.getLevel();
		}

		return error(model, "操作失败", "添加模块失败");
	}

	/**
	 * 跳转到修改页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, @PathVariable int id, Model model) {
		Module module = moduleService.findById(id);
		if (module != null) {
			List<Channel> channelList = channelService.listByParent(module.getLevel());
			
			model.addAttribute("channelList", channelList);
			model.addAttribute("module", module);
			return "module/edit";
		}

		return error(model, "操作失败", "模块信息不存在");
	}

	/**
	 * 修改模块信息
	 * 
	 * @param request
	 * @param module
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, Module module, Model model) {
		Manager manager = this.getSesionUser(request);
		module.setUpdateUser(manager.getUserCode());
		if (moduleService.update(module)) {
			model.addAttribute("module", module);
			return "redirect:modules/"+module.getLevel();
		}

		return error(model, "操作失败", "修改模块信息失败");
	}

	/**
	 * 模块信息详细
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		Module module = moduleService.findById(id);
		if (module != null) {
			model.addAttribute("module", module);
			return "module/details";
		}

		return error(model, "操作失败", "模块信息不存在");
	}
}
